import sys
import json

from cc.appcoin2.strategy.py_strat.strategy_multiprocessing import StrategyBase


class PrintStrategy(StrategyBase):
  def __init__(self, driver_config):
    super().__init__(driver_config)

  def on_book_feed(self, book):
    #auto& book = upd.book();
    #std::cout << "[" << std::setw(7) << MarketType_Name(upd.market()) << "," << std::setw(8)
    #          << ExchangeType_Name(upd.exchange()) << "] " << upd.ts().pre_read_available() << ","
    #          << upd.ts().pre_read_timestamp() << "," << upd.ts().post_read_available() << ","
    #          << upd.ts().post_read_timestamp() << " " << upd.ts().exchange_publish_timestamp()
    #          << ", " << Iso8601FromTimestamp(upd.timestamp()) << " " << std::setw(17)
    #          << upd.product().relative_norm() << "(" << book << ")" << std::endl;
    jd = book.as_dict()
    print(json.dumps(jd, indent=2))

  def on_trade_feed(self, trade):
    #auto& trade = upd.trade();
    #std::cout << "[" << std::setw(7) << MarketType_Name(upd.market()) << "," << std::setw(8)
    #          << ExchangeType_Name(upd.exchange()) << "] " << upd.ts().pre_read_available() << ","
    #          << upd.ts().pre_read_timestamp() << "," << upd.ts().post_read_available() << ","
    #          << upd.ts().post_read_timestamp() << "," << upd.ts().exchange_publish_timestamp()
    #          << "," << Iso8601FromTimestamp(upd.trade().timestamp) << " " << std::setw(17)
    #          << upd.product().relative_norm() << "(" << trade << ")" << std::endl;
    jd = trade.as_dict()
    print(json.dumps(jd, indent=2))

  def on_liquidation_feed(self, liquid):
    #auto& liquidation_order = upd.liquidation_order();
    #std::cout << "[" << std::setw(7) << MarketType_Name(upd.market()) << "," << std::setw(8)
    #          << ExchangeType_Name(upd.exchange()) << "] " << upd.ts().exchange_publish_timestamp()
    #          << ", " << Iso8601FromTimestamp(upd.timestamp()) << " " << std::setw(17)
    #          << upd.product().relative_norm() << "(" << liquidation_order << ")" << std::endl;
    jd = liquid.as_dict()
    print(json.dumps(jd, indent=2))

  def on_open_interest_feed(self, open_interest):
    #auto& open_interest = upd.open_interest();
    #std::cout << "[" << std::setw(7) << MarketType_Name(upd.market()) << "," << std::setw(8)
    #          << ExchangeType_Name(upd.exchange()) << "] " << upd.ts().exchange_publish_timestamp()
    #          << ", " << Iso8601FromTimestamp(upd.timestamp()) << " " << std::setw(17)
    #          << upd.product().relative_norm() << "(" << open_interest << ")" << std::endl;
    jd = open_interest.as_dict()
    print(json.dumps(jd, indent=2))

  def on_mark_price_feed(self, mark_price):
    #auto& mark_price = upd.mark_price();
    #std::cout << "[" << std::setw(7) << MarketType_Name(upd.market()) << "," << std::setw(8)
    #          << ExchangeType_Name(upd.exchange()) << "] " << upd.ts().exchange_publish_timestamp()
    #          << ", " << Iso8601FromTimestamp(upd.timestamp()) << " " << std::setw(17)
    #          << upd.product().relative_norm() << "(" << mark_price << ")" << std::endl;
    pass

  def on_index_feed(self, index):
    #auto& index = upd.index();
    #std::cout << "[" << std::setw(7) << MarketType_Name(upd.market()) << "," << std::setw(8)
    #          << ExchangeType_Name(upd.exchange()) << "] " << upd.ts().exchange_publish_timestamp()
    #          << ", " << Iso8601FromTimestamp(upd.timestamp()) << " " << std::setw(17)
    #          << upd.product().relative_norm() << "(" << index << ")" << std::endl;
    jd = index.as_dict()
    print(json.dumps(jd, indent=2))

  def on_funding_rate_feed(self, funding_rate):
    #auto& funding_rate = upd.funding_rate();
    #std::cout << "[" << std::setw(7) << MarketType_Name(upd.market()) << "," << std::setw(8)
    #          << ExchangeType_Name(upd.exchange()) << "] " << upd.ts().exchange_publish_timestamp()
    #          << ", " << Iso8601FromTimestamp(upd.timestamp()) << " " << std::setw(17)
    #          << upd.product().relative_norm() << "(" << funding_rate << ")" << std::endl;
    jd = funding_rate.as_dict()
    print(json.dumps(jd, indent=2))

  def on_iv_greeks_feed(self, iv_greeks):
    #auto& iv_greeks = upd.iv_greeks();
    #std::cout << "[" << std::setw(7) << MarketType_Name(upd.market()) << "," << std::setw(8)
    #          << ExchangeType_Name(upd.exchange()) << "] " << upd.ts().exchange_publish_timestamp()
    #          << ", " << Iso8601FromTimestamp(upd.timestamp()) << " " << std::setw(17)
    #          << upd.product().relative_norm() << "(" << iv_greeks << ")" << std::endl;
    jd = iv_greeks.as_dict()
    print(json.dumps(jd, indent=2))

  def on_nav_feed(self, nav):
    #auto& nav = upd.nav();
    #std::cout << "[" << std::setw(7) << MarketType_Name(upd.market()) << "," << std::setw(8)
    #          << ExchangeType_Name(upd.exchange()) << "] " << upd.ts().exchange_publish_timestamp()
    #          << ", " << Iso8601FromTimestamp(upd.timestamp()) << " " << std::setw(17)
    #          << upd.product().relative_norm() << "(" << nav << ")" << std::endl;
    jd = nav.as_dict()
    print(json.dumps(jd, indent=2))

  def on_kline_feed(self, kline):
    #auto& klines = upd.klines();
    #std::cout << "[" << std::setw(7) << MarketType_Name(upd.market()) << "," << std::setw(8)
    #          << ExchangeType_Name(upd.exchange()) << "] " << upd.ts().exchange_publish_timestamp()
    #          << ", " << Iso8601FromTimestamp(klines.Get().value()->timestamp) << " "
    #          << std::setw(17) << upd.product().relative_norm() << "(size " << klines.Size() << ", "
    #          << *(klines.Get().value()) << ")" << std::endl;
    jd = kline.as_dict()
    print(json.dumps(jd, indent=2))

  def on_wallet_feed(self, wallet):
    #auto& wallet = upd.wallet();
    #std::cout << "[" << std::setw(7) << MarketType_Name(upd.market()) << "," << std::setw(8)
    #          << ExchangeType_Name(upd.exchange()) << "] " << upd.ts().exchange_publish_timestamp()
    #          << ", " << Iso8601FromTimestamp(upd.timestamp()) << " " << std::setw(17)
    #          << upd.product().relative_norm() << "(" << wallet << ")" << std::endl;
    jd = wallet.as_dict()
    print(json.dumps(jd, indent=2))

  def on_market_analysis_feed(self, market_analysis):
    #auto& market_analysis = upd.market_analysis();
    #std::cout << "[" << std::setw(7) << MarketType_Name(upd.market()) << "," << std::setw(8)
    #          << ExchangeType_Name(upd.exchange()) << "] " << upd.ts().exchange_publish_timestamp()
    #          << ", " << Iso8601FromTimestamp(upd.timestamp()) << " " << std::setw(17)
    #          << upd.product().relative_norm() << "(" << market_analysis << ")" << std::endl;
    jd = market_analysis.as_dict()
    print(json.dumps(jd, indent=2))

strategy = PrintStrategy(driver_config=sys.argv[1])
strategy.run()
